/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package persistencia;

import excepciones.NonexistentEntityException;
import excepciones.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.UserTransaction;
import negocio.Sala;
import negocio.Seccion;

/**
 *
 * @author Diego
 */
public class SalaJpaController implements Serializable  {
    
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;
    
    public SalaJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    public SalaJpaController() {
        emf = Persistence.createEntityManagerFactory("entradasJpaPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    public void create(Sala sala)  throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(sala);
            em.getTransaction().commit();
        }
        catch(Exception ex){
            if(findSala(sala.getId()) != null){
                throw new PreexistingEntityException("Sala Existente",ex);
            }
            throw ex;
        }
        finally{
            if(em != null){
                em.close();
            }
        }
        
    }
    public void destroy(Integer id) throws NonexistentEntityException{
        EntityManager em = null;
        try{
           em = getEntityManager();
            em.getTransaction().begin();
            Sala sala;
            try{
                sala = em.getReference(Sala.class, id);                
            }
             catch(EntityNotFoundException ex){
                 throw new NonexistentEntityException("Sala Inexistente");    
            }     
            em.remove(sala);
            em.getTransaction().commit();
        }
        finally{
            if (em != null) {
                em.close();
            } 
        }                   
    }
    public void edit(Sala unaSala)   throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            unaSala = em.merge(unaSala);
            em.getTransaction().commit();
        }
        catch (Exception ex) {
             String msg = ex.getLocalizedMessage();
             if(msg == null || msg.length() == 0){
                 if(findSala(unaSala.getNombre()) == null){
                     throw new NonexistentEntityException("Sala Inexistente");
                 }
             }
        }
        finally {
            if (em != null) {
                em.close();
            }
        }
    }
     
     public Sala findSala(Integer id){
        EntityManager em = getEntityManager();
        try {
            return em.find(Sala.class, id);
        } finally {
            em.close();
        }
    }
    public Sala findSala(String nombre){
        EntityManager em = getEntityManager();
        try {          
            Query q = em.createQuery("select  object(o) from Sala as o where o.nombre = ?1 ");
            q.setParameter(1, nombre);
            return (Sala) q.getSingleResult();
            
        }
        catch(NoResultException e) {
            return null; 
        }
        finally {
            em.close();
        }   
    } 
     
    public List<Sala> findSalaEntities() {
        return findSalaEntities(true, -1, -1);
    }
    private  List  findSalaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Sala as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public int getSeccionCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Seccion as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public int getButacaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Butaca as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
